package cn.tedu.pet.passport.controller;


import cn.tedu.pet.passport.pojo.param.AdminAddNewParam;
import cn.tedu.pet.passport.pojo.param.AdminUpdateInfoParam;
import cn.tedu.pet.passport.pojo.param.PermissionAddNewParam;
import cn.tedu.pet.passport.pojo.param.PermissionUpdateInfoParam;
import cn.tedu.pet.passport.pojo.vo.AdminListItemVO;
import cn.tedu.pet.passport.pojo.vo.AdminStandardVO;
import cn.tedu.pet.passport.pojo.vo.PageData;
import cn.tedu.pet.passport.pojo.vo.PermissionListItemVO;
import cn.tedu.pet.passport.security.LoginPrincipal;
import cn.tedu.pet.passport.service.IPermissionService;
import cn.tedu.pet.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/permission")
@Validated
@Api(tags = "03. 权限管理模块")
public class PermissionController  {
    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/list")
    @ApiOperation("查询权限列表")
    @ApiOperationSupport(order =100)
    public JsonResult list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal,
                           @Range(min = 1,message = "查询权限表失败，请提供正确的页面值") Integer page) {
        log.debug("当事人的用户名：{}", loginPrincipal.getUsername());
        log.debug("当事人的ID：{}", loginPrincipal.getId());
        log.debug("开始处理【查询权限列表】的业务页码：{}",page);
        if(page==null||page<1){
            page=1;
        }
        PageData<PermissionListItemVO> pageData= permissionService.list(page);
        return JsonResult.ok(pageData);
    }

    @PostMapping("/add-new")
    @ApiOperation("添加权限")
    @ApiOperationSupport(order = 200)
    public JsonResult addNew(@Valid PermissionAddNewParam permissionAddNewParam) {
        log.debug("开始处理【添加管理员】的请求，参数：{}", permissionAddNewParam);
        permissionService.addNew(permissionAddNewParam);
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询管理员权限详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long"),
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询管理员权限详情失败，请提交合法的ID值！")
                                      @RequestParam Long id)throws Exception{
        log.debug("开始处理【根据ID查询管理员权限详情】的请求，参数：{}", id);
        PageData<PermissionListItemVO> pageData =permissionService.getListById(id);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standardById")
    @ApiOperation("根据ID查询权限详情")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "long"),
    })
    public JsonResult getStandard(@Range(min = 1, message = "根据ID查询权限详情失败，请提交合法的ID值！")
                                      @RequestParam Long id)throws Exception{
        log.debug("开始处理【根据ID查询权限详情】的请求，参数：{}", id);
       PermissionListItemVO pageData =permissionService.getStandardById(id);
        return JsonResult.ok(pageData);
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除权限")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "long"),
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除权限失败，请提交合法的ID值！")
                                 @RequestParam Long id)throws Exception{
        log.debug("开始处理【根据ID删除权限详情】的请求，参数：{}", id);
        permissionService.deleteById(id);
        return JsonResult.ok();
    }


    @PostMapping("/update")
    @ApiOperation("修改权限")

    @ApiOperationSupport(order = 300)
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "根据ID修改失败，请提交合法的ID值！")
                                             Long id, @Valid PermissionUpdateInfoParam permissionUpdateInfoParam)throws Exception {
        log.debug("开始处理【修改管理员详情】的业务，参数：{}",permissionUpdateInfoParam );
        permissionService. updateInfoById(id,permissionUpdateInfoParam);
        return JsonResult.ok();
    }

}
